﻿$(function () {
    $('.timepicker').timepicker({
        showPeriod: true,
        showLeadingZero: false
    });

    $("#submit").click(function () {
        TrySubmit();
    });

    $("#body").keypress(function (event) {
        if (event.which == '13') {
            TrySubmit();
        }
    });

    $("#cancel").click(function () {
        location.href = "/Events";
    });

    $("#delete").click(function () {
        TryDelete();
    });

    $("#IsAllDay,#IsRepeat").change(function () {
        ShowHideInputs(false);
    });

    $("#IsRepeatSummaryEdit").click(function () {
        showModal($("#repeat"));
        ShowHideInputsRepeat();
    });

    $("#RepeatType,#RepeatEvery,input[name='RepeatEndType'],input[name='RepeatMonthlyBy']").change(function () {
        ShowHideInputsRepeat();
    });

    $("#RepeatWeeklySun,#RepeatWeeklyMon,#RepeatWeeklyTeu,#RepeatWeeklyWed,#RepeatWeeklyThu,#RepeatWeeklyFri,#RepeatWeeklySat").change(function () {
        ShowHideInputsRepeat();
    });

    $("#repeatsubmit").click(function () {
        hideModal($("#repeat"));
    });

    $("#erthisonly, #erfollowing, #erall").click(function () {
        btn = $(this).attr('id');
        if (btn == "erthisonly") {
            $("#EditRepeatType").val(1);
        } else if (btn == "erfollowing") {
            $("#EditRepeatType").val(2);
        } else if (btn == "erall") {
            $("#EditRepeatType").val(3);
        }
        hideModal($("#repeatchange"));

        if ($("#EditRepeatSubmitCancel").val() == "1") {
            Submit();
        } else {
            Delete();
        }
    });

    $("#ercancel").click(function () {
        hideModal($("#repeatchange"));
    });

    ShowHideInputs(true);
    ShowHideInputsRepeat();
});

function ShowHideInputs(isLoad) {
    var OccuranceID = $("#OccuranceID").val();

    var IsAllDay = $("#IsAllDay").is(":checked");
    if (IsAllDay) {
        $("#StartDateTime").hide();
        $("#EndDateTime").hide();
    } else {
        $("#StartDateTime").show();
        $("#EndDateTime").show();
    }

    if (OccuranceID == 0) {
        $("#delete").hide();
    } else {
        $("#delete").show();
    }
    
    var IsRepeat = $("#IsRepeat").is(":checked");
    if (IsRepeat) {
        if (!isLoad) {
            showModal($("#repeat"));
        }
        ShowHideInputsRepeat();
        $("#IsRepeatSummary, #IsRepeatSummaryEdit").show();
    } else {
        $("#IsRepeatSummary, #IsRepeatSummaryEdit").hide();
    }
}


function ShowHideInputsRepeat(){
    var RepeatType = $("#RepeatType").val();
    var OccuranceID = $("#OccuranceID").val();
    
    $("#repeatweekly").hide();
    $("#repeatmonthly").hide();

    if(RepeatType == "1") {
        $("#RepeatEveryIndicator").html("days");
    } else if(RepeatType == "2") {
        $("#RepeatEveryIndicator").html("weeks");
        $("#repeatweekly").show();
    } else if(RepeatType == "3") {
        $("#RepeatEveryIndicator").html("months");
        $("#repeatmonthly").show();
    } else if(RepeatType == "4") {
        $("#RepeatEveryIndicator").html("years");
    }

    if(OccuranceID == 0){
        $("#RepeatStartsOn").val($("#StartDate").val());
        $("#RepeatStartsOn").attr("disabled", "disabled");
    }

    if($("input[type='checkbox'][name^='RepeatWeekly']:checked").length == 0){
        var d = new Date($("#RepeatStartsOn").val());
        var dayofweek = d.getDay();
        if(dayofweek == 0){
            $("#RepeatWeeklySun").attr("checked", true);
        } else if(dayofweek == 1){
            $("#RepeatWeeklyMon").attr("checked", true);
        } else if(dayofweek == 2){
            $("#RepeatWeeklyTeu").attr("checked", true);
        } else if(dayofweek == 3){
            $("#RepeatWeeklyWed").attr("checked", true);
        } else if(dayofweek == 4){
            $("#RepeatWeeklyThu").attr("checked", true);
        } else if(dayofweek == 5){
            $("#RepeatWeeklyFri").attr("checked", true);
        } else if(dayofweek == 6){
            $("#RepeatWeeklySat").attr("checked", true);
        }
    }    
    
    var RepeatEndType = $("input[name='RepeatEndType']:checked").val();
    if(RepeatEndType == "2"){
        $("#RepeatEndOccurences").removeAttr("disabled"); 
    } else {
        $("#RepeatEndOccurences").attr("disabled", "disabled");    
    }
    if(RepeatEndType == "3"){
        $("#RepeatEndDate").removeAttr("disabled"); 
    } else {
        $("#RepeatEndDate").attr("disabled", "disabled");
    }

    if ($("#RepeatEndDate").val() == "1/1/0001 12:00:00 AM") {
        var de = new Date($("#RepeatStartsOn").val());
        $("#RepeatEndDate").val(de.getMonth() + "/" + de.getDate() + "/" + (de.getFullYear() + 5));
    }

    GenerateRepeatSummary();
}

function GenerateRepeatSummary(){
    var summary;
    var StartDate = $("#StartDate").val();
    var RepeatStartsOn = $("#RepeatStartsOn").val();
    var RepeatType = $("#RepeatType").val();
    var RepeatEvery = $("#RepeatEvery").val();
    var RepeatEndType = $("input[name='RepeatEndType']:checked").val();
    var RepeatEndOccurences = $("#RepeatEndOccurences").val();
    var RepeatEndDate = $("#RepeatEndDate").val();
    var RepeatWeeklySun = $("#RepeatWeeklySun").is(":checked");
    var RepeatWeeklyMon = $("#RepeatWeeklyMon").is(":checked");
    var RepeatWeeklyTeu = $("#RepeatWeeklyTeu").is(":checked");
    var RepeatWeeklyWed = $("#RepeatWeeklyWed").is(":checked");
    var RepeatWeeklyThu = $("#RepeatWeeklyThu").is(":checked");
    var RepeatWeeklyFri = $("#RepeatWeeklyFri").is(":checked");
    var RepeatWeeklySat = $("#RepeatWeeklySat").is(":checked");
    var RepeatMonthlyBy = $("input[name='RepeatMonthlyBy']:checked").val();

    if (RepeatType == "1") {
        if(RepeatEvery == "1"){
            summary = "Daily";
        } else {
            summary = "Every " + RepeatEvery + " days";   
        }
    } else if (RepeatType == "2"){
        if(RepeatEvery == "1"){
            summary = "Weekly";
        } else {
            summary = "Every " + RepeatEvery + " weeks";   
        }
        summary += " on ";
        summary += RepeatWeeklySun ? "Sunday ": "";
        summary += RepeatWeeklyMon ? "Monday ": "";
        summary += RepeatWeeklyTeu ? "Teusday ": "";
        summary += RepeatWeeklyWed ? "Wednesday ": "";
        summary += RepeatWeeklyThu ? "Thursday ": "";
        summary += RepeatWeeklyFri ? "Friday ": "";
        summary += RepeatWeeklySat ? "Saturday ": "";

    } else if (RepeatType == "3"){
        if(RepeatEvery == "1"){
            summary = "Monthly";
        } else {
            summary = "Every " + RepeatEvery + " months";   
        }
        summary += " on ";
        var d = new Date(RepeatStartsOn);
        if(RepeatMonthlyBy == "1"){
            summary += " day " + d.getDate();
        } else if (RepeatMonthlyBy == "2"){
            summary += GetDayOccurance(d) + " " + GetDayName(d);
        }
    } else if (RepeatType == "4"){
        if(RepeatEvery == "1"){
            summary = "Annually";
        } else {
            summary = "Every " + RepeatEvery + " years";   
        }
        summary += " on ";
        var de = new Date(RepeatStartsOn);
        summary+= de.getMonth() + "/" + de.getDate();
    }

    if(RepeatEndType == "2") {
        summary += ", " + RepeatEndOccurences + " times";
    } else if(RepeatEndType == "3") {
        summary += ", until " + RepeatEndDate;
    }

    $("#repeatsummary").html(summary);
    $("#IsRepeatSummary").html(summary);
}

function GetDayOccurance(d){
    var dayofweek = d.getDay();
    var date = d.getDate();
    var occurance = Math.floor(date / 7) + 1;
    
    if(occurance == 1){
        return "first";
    } else if(occurance == 2){
        return "second";
    } else if(occurance == 3){
        return "third";
    } else if(occurance == 4){
        return "fourth";
    } else if (occurance == 5) {
        return "fifth";
    }
}

function GetDayName(d){
    var dayofweek = d.getDay();
    if(dayofweek == 0){
        return "Sunday";
    } else if(dayofweek == 1){
        return "Monday";
    } else if(dayofweek == 2){
        return "Teusday";
    } else if(dayofweek == 3){
        return "Wednesday";
    } else if(dayofweek == 4){
        return "Thursday";
    } else if(dayofweek == 5){
        return "Friday";
    } else if(dayofweek == 6){
        return "Saturday";
    } 
}

function TrySubmit() {
    var OccuranceID = $("#OccuranceID").val();
    var IsRepeat = $("#IsRepeat").is(":checked");

    if (OccuranceID == 0 || !IsRepeat) {
        Submit();
    } else {
        $("#EditRepeatSubmitCancel").val(1);
        showModal($("#repeatchange"));
    }
}

function TryDelete() {
    var OccuranceID = $("#OccuranceID").val();
    var IsRepeat = $("#IsRepeat").is(":checked");

    if (OccuranceID == 0 || !IsRepeat) {
        Delete();
    } else {
        $("#EditRepeatSubmitCancel").val(2);
        showModal($("#repeatchange"));
    }
}

function Submit() {
    $("#submit").hide();
    var input = {
        OccuranceID: $("#OccuranceID").val(),
        CalendarID: $("#Calendar").val(),
        Title: $("#Title").val(),
        ShortDescription: $("#ShortDescription").val(),
        Location: $("#Location").val(),
        IsAllDay: $("#IsAllDay").is(":checked"),
        StartDate: $("#StartDate").val(),
        StartDateTime: $("#StartDateTime").val(),
        EndDate: $("#EndDate").val(),
        EndDateTime: $("#EndDateTime").val(),
        TimeZone: $("#TimeZone").val(),
        Description: $("#Description").val(),
        IsRepeat: $("#IsRepeat").is(":checked"),
        EditRepeatType: $("#EditRepeatType").val(),
        RepeatType: $("#RepeatType").val(),
        RepeatEndType: $("input[name='RepeatEndType']:checked").val(),
        RepeatEndOccurences: $("#RepeatEndOccurences").val(),
        RepeatEndDate: $("#RepeatEndDate").val(),
        RepeatEvery: $("#RepeatEvery").val(),
        RepeatStartsOn: $("#RepeatStartsOn").val(),
        RepeatWeeklySun: $("#RepeatWeeklySun").is(":checked"),
        RepeatWeeklyMon: $("#RepeatWeeklyMon").is(":checked"),
        RepeatWeeklyTeu: $("#RepeatWeeklyTeu").is(":checked"),
        RepeatWeeklyWed: $("#RepeatWeeklyWed").is(":checked"),
        RepeatWeeklyThu: $("#RepeatWeeklyThu").is(":checked"),
        RepeatWeeklyFri: $("#RepeatWeeklyFri").is(":checked"),
        RepeatWeeklySat: $("#RepeatWeeklySat").is(":checked"),
        RepeatMonthlyBy: $("input[name='RepeatMonthlyBy']:checked").val()
    };

    Ajax({
        url: "/Events/SaveEvent",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                ShowValidationErrors(data, { CalendarID : "Calendar" });
            } else if (data.IsSaved == true) {
                location.href = "/Events?msg=1";
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
}

function Delete() {
    var input = {
        OccuranceID: $("#OccuranceID").val(),
        StartDate: $("#StartDate").val(),
        IsRepeat: $("#IsRepeat").is(":checked"),
        EditRepeatType: $("#EditRepeatType").val()
    };

    Ajax({
        url: "/Events/DeleteEvent",
        data: JSON.stringify(input),
        success: function (data) {
             location.href = "/Events?msg=2";
        }
    });
}